package com.dlc.apk.ui.fragment

import android.text.TextUtils
import com.dlc.apk.R
import com.dlc.apk.base.BaseFragment
import com.dlc.apk.databinding.FragmentLoginBinding
import com.dlc.apk.service.staff.IStaffService
import com.dlc.apk.service.staff.impl.StaffService
import com.dlc.apk.ui.activity.MainActivity
import com.dlc.apk.ui.model.AppForm
import com.dlc.apk.ui.model.LoginFrom
import com.dlc.apk.utils.DeviceUtil
import com.dlc.apk.utils.ViewShakeHelper

class LoginFragment : BaseFragment() {
    private lateinit var fragmentBinding: FragmentLoginBinding
    private lateinit var appForm: AppForm
    private lateinit var staffService: IStaffService
    private lateinit var loginForm: LoginFrom

    override fun getLayoutId(): Int {
        return R.layout.fragment_login
    }

    override fun initViewModels() {
        loginForm = LoginFrom()
        appForm = AppForm()
        appForm.versionName.set(DeviceUtil.packageVersion)
    }

    override fun initServices() {
        staffService = StaffService()
    }

    override fun init() {
        fragmentBinding = getViewDataBinding<FragmentLoginBinding>()!!
        fragmentBinding.loginFrom = loginForm
        fragmentBinding.app = appForm

        fragmentBinding.tvLogin.setOnClickListener {
            toLogin()
        }

        (getParentActivity() as MainActivity).showLogoBar()
    }

    private fun toLogin() {
        if (TextUtils.isEmpty(loginForm.account.get())) {
            showToast("请输入账号")
            ViewShakeHelper.getInstance().shake(fragmentBinding.llAccount)
            return
        }
        if (TextUtils.isEmpty(loginForm.password.get())) {
            showToast("请输入密码")
            ViewShakeHelper.getInstance().shake(fragmentBinding.llPassword)
            return
        }
        if ("Apk" != loginForm.account.get()) {
            showToast("账号不正确")
            return
        }
        if ("123456" != loginForm.password.get()) {
            showToast("密码不正确")
            return
        }
        //进入运维测试界面
        (getParentActivity() as MainActivity).showTestingFragment(false)
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    override fun onDestroyView() {
        super.onDestroyView()
    }

    override fun onDetach() {
        super.onDetach()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden) {

        }
    }

    companion object {

    }
}